package client.gui;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import client.GetFileFromServer;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.*;

import messages.*;
import sharedspace.*;
/**
 * This JPanel is a panel for Student to show him his grades
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *
 */
public class GUI_Student_ViewGrades extends JPanel{
	private static final long serialVersionUID = 1L;
	private Student student;
	private Client_GUI ms;
	private JLabel titleLabel = null;
	private JLabel avgLabel = null;
	private JButton testButton = null;
	public JButton backButton = null;
	private GUI_Student_ViewTest testFrame;
	private JScrollPane studentGradeScroller = null;
	private JTable studentGradeTable = null;
	private DefaultTableModel model = null;
	private List<ApprovingGrade> aList = null;
	public boolean b = true;
	
	public GUI_Student_ViewGrades(Client_GUI ms,Student student)
	{
		super();
		this.student = student;
		this.ms = ms;
		getStudentGrade();
		if (aList.size() == 0)
		{
			JOptionPane.showMessageDialog(null, "No grades in database");
			b = false;
		}
		else this.initialized();
	}
	
	public void initialized()
	{
		backButton = new JButton("Back");
		backButton.setFont(new Font("Arial", Font.PLAIN, 16));
		backButton.setBounds(118, 582, 99, 34);
		
		titleLabel = new JLabel("Grades");
		titleLabel.setForeground(new Color(0, 0, 204));
		titleLabel.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 50));
		titleLabel.setSize(185, 78);
		titleLabel.setLocation(404, 11);
		
		avgLabel = new JLabel("Average grade : "+aList.get(0).getStudentInTest().getStudent().getStudentAvg());
		avgLabel.setForeground(new Color(0, 0, 204));
		avgLabel.setFont(new Font("Arial", Font.PLAIN, 16));
		avgLabel.setSize(235, 34);
		avgLabel.setLocation(50, 109);
		
		GUI_ImagePanel background =new GUI_ImagePanel(new ImageIcon("images/background1.jpg").getImage());
		background.setLocation(0, 0);
		background.setSize(new Dimension(1000, 700));
		
		this.setLayout(null);
		this.setSize(1000, 700);
		this.add(backButton);
		this.add(titleLabel);
		this.add(getStudentGradeTable());
		this.add(getTestButton());
		this.add(avgLabel);
		this.add(background);
	}
	
	public JScrollPane getStudentGradeTable()
	{
		if (studentGradeScroller == null)
		{
			String colunmNames[] = {"ExecuteTestID","TestID","Subject Name","Course Name","Student Name","Computer Grade","Final Grade","Manual Test Location"};
			String[][] tableModel = new String[aList.size()][8];
				for (int i = 0; i<aList.size(); i++)
				{
					tableModel[i][0] = aList.get(i).getExecuteTest().getExecuteTestID();
					tableModel[i][1] = aList.get(i).getExecuteTest().getTest().getTestID();
					tableModel[i][2] = aList.get(i).getExecuteTest().getTest().getSubject().getSubjectName();
					tableModel[i][3] = aList.get(i).getExecuteTest().getTest().getCourse().getCourseName();
					tableModel[i][4] = aList.get(i).getStudentInTest().getStudent().getUserFirstName()+" "
							       	   +aList.get(i).getStudentInTest().getStudent().getUserLastName();
					tableModel[i][5] = Integer.toString(aList.get(i).getStudentInTest().getStudentComputerGrade());
					tableModel[i][6] = Integer.toString(aList.get(i).getStudentFianlGrade());
					if (aList.get(i).getStudentInTest().getWordTestCheckedPath()== null)
						tableModel[i][7] = "Test is computerized";
					else tableModel[i][7] = aList.get(i).getStudentInTest().getWordTestCheckedPath(); 
				}
		
			model = new DefaultTableModel(tableModel, colunmNames);
			
			studentGradeTable = new JTable(model){
				   /**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public boolean isCellEditable(int rowIndex, int colIndex) {
				   return false; //Disallow the editing of any cell
				   }
				   };
			studentGradeTable.setFont(new Font("Tahoma", Font.PLAIN, 14));
			studentGradeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			studentGradeTable.getTableHeader().setReorderingAllowed(false);
			studentGradeScroller = new JScrollPane(studentGradeTable);
			studentGradeScroller.setBounds(50, 150, 900, 400);
			
		}
		return studentGradeScroller;
	}
	
	/**
	 * get list of grades of student
	 */
	public void getStudentGrade()
	{
		try
		{
			MessageStudentGrades msgsg = new MessageStudentGrades(student);
			ms.client.sendToServer(msgsg);//sending student info to server
			//get student final grades from server
			MessageStudentGradesReply msgsgrep = (MessageStudentGradesReply)ms.client.GetMessage();
			aList = msgsgrep.getgList();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public JButton getTestButton()
	{
		testButton = new JButton("Show Test ");
		testButton.setBounds(449, 113, 100, 30);
		testButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println(studentGradeTable.getSelectedRow());
				if (aList.size()==0)
					JOptionPane.showMessageDialog(null, "No Test");
				else
				{
					int i = studentGradeTable.getSelectedRow();
					if (aList.get(i).getStudentInTest().getWordTestCheckedPath()==null)
					{
						testFrame = new GUI_Student_ViewTest(aList.get(i));
						testFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
						testFrame.setResizable(false);
						testFrame.setVisible(true);
					}
					else
					{
						MessageGetStudentWordFile msggswf = new MessageGetStudentWordFile(aList.get(i).getStudentInTest().getWordTestCheckedPath());
						try {
							ms.client.sendToServer(msggswf);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						MyFile myFile = (MyFile)ms.client.GetMessage();
						File file = FileHandler.saveFileDialog("Save Test");
						String path = file.getAbsolutePath();
					    GetFileFromServer gffs = new GetFileFromServer(path);
						gffs.getFileFromServer(myFile);
								
					    
					}
				}
			}
		});
		
		
		return testButton;
	}

}
